<?php
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of process
 *
 * @author Nhan Nguyen
 */
defined('IN_ADMIN') or die('Hacking Attempt!');

final class func {
    public static $_sUrl = array();

    public static function setDefaultTimeZone(){
        date_default_timezone_set('Asia/Ho_Chi_Minh');
    }

    public static function cleanString($sString){
        return htmlspecialchars($sString, ENT_QUOTES);
    }

    public static function cleanEmail($sEmail){
        $sPattern = '/^[^@\s<&>]+@([-a-z0-9]+\.)+[a-z]{2,}$/i';
        if(preg_match($sPattern, $sEmail))
                return $sEmail;
        else
            return '';
    }

    public static function isLogin(){
        if(!isset($_SESSION[SESSION_NAME]))
            func::redirect(func::makeUrl('login'));
    }

    public static function isUser(){
        if(isset($_SESSION[SESSION_NAME]))
            return true;
        else
            return false;
    }

    public static function getDate($iTime){
        return date("d/m/y H:i:s", $iTime);
    }

    public static function displayDate(){
        $aDate = getdate();
        return ('ngày '.$aDate['mday'].' tháng '.$aDate['mon'].' năm '.$aDate['year']);
    }

    public static function getCurrentUrl(){
        //module name
        func::$_sUrl['mod'] = isset($_GET['mod']) ? $_GET['mod'] : 'login';
        //action
        func::$_sUrl['act'] = isset($_GET['act']) ? $_GET['act'] : '';
        //id
        func::$_sUrl['id'] = isset($_GET['id']) ? $_GET['id'] : '';

        return func::$_sUrl;
    }

    public static function makeUrl($sMod = null, $sAct = null, $iId = null){
        if($sMod != null){
            if($sAct != null){
                if($iId != null){
                    return 'index.php?mod='. $sMod .'&act='. $sAct .'&id='. $iId;
                }else{
                    return 'index.php?mod='. $sMod .'&act='. $sAct;
                }
            }else{
                return 'index.php?mod='. $sMod;
            }
        } else
            return 'index.php?';
    }

    public static function redirect($sUrl){
        //clear buffer
        ob_start();
        //redirect page
        header('Location: '. SITE_URL . $sUrl);
    }

    public static function numberFormat($iMoney){
        return number_format($iMoney);
    }

    public static function replaceNumberFormat($aVals){
        for($i=0; $i < (int)((count($aVals, 1) - 3) / 3); $i++){
            $aVals['quantity'][$i] = func::numberFormatInput($aVals['quantity'][$i]);
            $aVals['price'][$i] = func::numberFormatInput($aVals['price'][$i]);
        }
        return $aVals;
    }

    public static function numberFormatInput($iMoney){
        return str_replace(',', '', $iMoney);
    }

    public static function stringToLower($sString){
        return preg_match(' ', strtolower($sString));        
    }

    public static function getSetting(){
        $sSql = 'SELECT * FROM setting WHERE settingId=1';
        $aResult = mysql::querySql($sSql);
        $aSetting = mysql::fetchSql($aResult);

        return $aSetting;
    }

    public static function main(){
        $aUrl = func::getCurrentUrl();
        
        include MODULE_ADMIN_DIR.$aUrl['act'].ucfirst($aUrl['mod']).EXT;
        include TEMPLATE_ADMIN_DIR.$aUrl['act'].ucfirst($aUrl['mod']).HTML_EXT;
    }

    public static function header(){
        echo "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">
<html>
  <head>
    <title></title>
    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
    <link type=\"text/css\" rel=\"stylesheet\" href=\"".CSS_DIR."style.css\" />
    <script type=\"text/javascript\" src=\"".JSCRIPT_DIR."/jquery.min.js\"></script>
    <script type=\"text/javascript\" src=\"".JSCRIPT_DIR."/jquery.jclock.js\"></script>
    <script type=\"text/javascript\" src=\"".JSCRIPT_DIR."/script.js\"></script>
  </head>
  <body>
      <div id=\"container\">
          <div id=\"header_container\" class=\"body_container\">";
                if(func::isUser()){
                    include MODULE_ADMIN_DIR.'header'.EXT;
                    include TEMPLATE_ADMIN_DIR.'header'.HTML_EXT;
                }
          echo "</div>
          <div class=\"clear\"></div>
          <div id=\"menu_container\" class=\"body_container\">";
                if(func::isUser()){
                    include MODULE_ADMIN_DIR.'menu'.EXT;
                    include TEMPLATE_ADMIN_DIR.'menu'.HTML_EXT;
                }
          echo "</div>
          <div class=\"clear\"></div>
          <div id=\"main_container\" class=\"body_container\">
              <div id=\"content_container\">";
    }
    public static function footer(){
        echo "</div>
              <div id=\"sidebar_container\">";
              if(func::isUser()){
                  include MODULE_ADMIN_DIR.'sidebar'.EXT;
                  include TEMPLATE_ADMIN_DIR.'sidebar'.HTML_EXT;
              }
        echo "</div>
          </div>
          <div class=\"clear\"></div>
          <div id=\"footer_container\" class=\"body_container\">";
                include MODULE_ADMIN_DIR.'footer'.EXT;
                include TEMPLATE_ADMIN_DIR.'footer'.HTML_EXT;
        echo "</div>
      </div>
  </body>
</html>";
    }
}
?>
